<template>
  <div>
    <el-form-item label="值信息">
      <el-select v-model="localData.bindAttrs.value" placeholder="选择值信息">
        <el-option v-for="item in userInfoList" :key="item.dictValue" :label="item.dictLabel" :value="item.dictValue" />
      </el-select>
    </el-form-item>
    <el-form-item label="显示信息">
      <el-select v-model="localData.userInfo" placeholder="选择显示信息">
        <el-option v-for="item in userInfoList" :key="item.dictValue" :label="item.dictLabel" :value="item.dictValue" />
      </el-select>
    </el-form-item>
    <el-form-item v-for="item in bindAttrs" :key="item.value" :label="item.label">
      <el-cascader
        :props="CascaderProps" :options="attrList" v-model="localData.bindAttrs[item.value]"
        placeholder="请选择属性" filterable clearable :show-all-levels="false">
        <template #default="{ data: opt }">
          <span>{{ `${opt.entityDesc}(${opt.entityName})` }}</span>
        </template>
      </el-cascader>
    </el-form-item>
    <el-form-item label="显示格式">
      <el-select v-model="localData.format" placeholder="请选择显示格式">
        <el-option v-for="opt in formatOptions" :label="opt.label" :value="opt.value" :key="opt.value" />
      </el-select>
    </el-form-item>
  </div>
</template>
<script>
export default {
  name: 'UserInfoConfig',
  props: {
    data: {
      type: Object,
      default: () => { }
    },
    attrList: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      userType: '1',
      userInfoList: [],
      userInfo: '',
      localData: {},
      CascaderProps: {
        value: 'entityName',
        label: 'entityDescName',
        children: 'childList',
        emitPath: false
      },
      bindAttrs: [
        {
          label: '显示绑定属性',
          value: 'userInfo'
        },
      ],
      formatOptions: [
        {
          label: '{用户ID}',
          value: 'userId'
        },
        {
          label: '{用户信息}',
          value: 'userInfo'
        },
        {
          label: '{用户信息}({用户ID})',
          value: 'userInfo(userId)'
        },
      ]
    }
  },
  watch: {
    data: {
      handler(newVal) {
        if (newVal) {
          this.localData = newVal
          this.init()
        }
      },
      deep: true,
      immediate: true
    },
    localData: {
      handler(newVal) {
        this.$emit('update:data', newVal)
      },
      deep: true
    }
  },
  created() {
    this.userInfo = this.data.userInfo
    this.getDicts('code_sys_user_info').then((res) => {
      this.userInfoList = res.data
    })
  },
  methods: {
    init() {
      if (this.localData && !this.localData.bindAttrs) {
        this.localData.bindAttrs = {}
      }
    }
  }
}
</script>

<style scoped></style>
